import subprocess
import shutil
import os

def cleanup_report():
    """
    清理 report 文件夹
    """
    report_dir = "report"
    if os.path.exists(report_dir):
        shutil.rmtree(report_dir)
        print(f"Deleted {report_dir} directory.")
    else:
        print(f"{report_dir} directory does not exist.")

def run_tests():
    """
    运行 pytest 测试
    """
    # 调用 pytest 执行测试
    # result = subprocess.run(["pytest", "--alluredir=report"], capture_output=True, text=True)
    result = subprocess.run(["pytest", "--alluredir=report"], capture_output=True, encoding='utf-8', errors='ignore')
    # 打印测试结果
    print("Test Output:")
    print(result.stdout)

    if result.returncode != 0:
        print("Tests failed!")
    else:
        print("All tests passed!")

if __name__ == "__main__":
    # 清理 report 文件夹
    cleanup_report()
    # 运行测试
    run_tests()

    #生成测试报告
